﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Models;
using SlamDunk.MES.Presentation;
using SlamDunk.MES.Presentation.Models.NotifyModels;
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.StockInOrder
{
    /// <summary>
    /// 入库单据补打条码ViewModel
    /// </summary>
    public class ReprintUniqueBarcodeViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ReprintUniqueBarcodeViewModel()
        {

        }

        #endregion

        #region 属性
        #region 条码号
        /// <summary>
        /// 条码号
        /// </summary>
        [DependencyProperty]
        public string RelatedBarcodeNo { get; set; }
        #endregion

        #region 条码集合
        /// <summary>
        /// 条码集合
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<string>> BarcodeNos { get; set; }

        [DependencyProperty]
        public ObservableCollection<Wrap<string>> BarcodeNoList { get; set; }
        #endregion

        #region 入库单明细
        /// <summary>
        /// 入库单明细
        /// </summary>
        [DependencyProperty]
        public StockInOrderDetailNm StockInOrderDetailNm { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(StockInOrderInfo stockInOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(StockInOrderDetailNm stockInOrderDetailNm)
        {
            this.StockInOrderDetailNm = stockInOrderDetailNm;
            BarcodeNoList = new ObservableCollection<Wrap<string>>(this.StockInOrderDetailNm.RelatedBarcodeNos.Select(x => x.Wrap()));
            BarcodeNos = new ObservableCollection<Wrap<string>>(this.StockInOrderDetailNm.RelatedBarcodeNos.Select(x => x.Wrap()));
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            string[] checkedRoles = this.BarcodeNos.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoles.Any())
            {
                MessageBox.Show("请至少勾选一条物料条码！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            this.Busy();
            foreach (var barcodeNo in checkedRoles)
            {
                string BarcodeName = this.StockInOrderDetailNm.BarcodeModeType ? "唯一码" : "通用码";
                string str = $@"^XA
^CI28
^CW1,E:SIMSUN.FNT
^PQ1,0,0.5,Y
^FTT
^FO60,60^BQN,4,11^FDLA,{barcodeNo}^FS
^FO60,340^A1N,36,36^FD条码号：{barcodeNo}^FS
^FO60,20^A1,36,36^FD{this.StockInOrderDetailNm.MaterialName}^FS
^FO295,75^A1,30,30^FD规格：{this.StockInOrderDetailNm.Specification}^FS
^FO295,110^A1,30,30^FD批号：{this.StockInOrderDetailNm.ProductionBatchNo}^FS
^FO295,150^A1,30,30^FD生产日期：{this.StockInOrderDetailNm.ProducedDate}^FS
^FO295,190^A1,30,30^FD接收日期：{this.StockInOrderDetailNm.ReceptionDate}^FS
^FO295,210^A1,30,30^条码类型：{BarcodeName}^FS
^FO295,240^A1,30,30^FD打印日期：{DateTime.Now:yyyy-MM-dd}^FS
^XZ
";
                string printName = ConfigurationManager.AppSettings[Constants.PrintNameKey];
                //PrinterMediator.SendInstructionsToPrinter(printName, str);
                //TODO 打印
            }
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #region 搜索 —— async void LoadBarcodeNo()
        /// <summary>
        /// 搜索
        /// </summary>
        public async void LoadBarcodeNo()
        {
            this.Busy();
            if (string.IsNullOrEmpty(RelatedBarcodeNo))
            {
                BarcodeNos = BarcodeNoList;
            }
            else
            {
                var BarcodeNosNew = BarcodeNos.Where(x => x.Model.Contains(RelatedBarcodeNo));
                BarcodeNos = new ObservableCollection<Wrap<string>>(BarcodeNosNew);
            }
            this.Idle();
        }
        #endregion

        #endregion
    }
}
